﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TreeViewWithViewModelDemo.FolderTree.DataModel;
using TreeViewWithViewModelDemo.FolderTree.ViewModel;

namespace TreeViewWithViewModelDemo.FolderTree
{
    /// <summary>
    /// FolderTreeDemo.xaml 的交互逻辑
    /// </summary>
    public partial class FolderTreeDemo : UserControl
    {
        
        public FolderTreeDemo()
        {
            InitializeComponent();

            this.DataContext = new FolderTreeVIewModel(GetFloders());
        }

        private MyFolder GetFloders()
        {
            MyFolder rootFolder = new MyFolder();
            rootFolder.FloderName = "项目";
            rootFolder.FullPath = @"E:\Win7Desktop\项目\Xiaguang\项目";
            

            string[] dirs = System.IO.Directory.GetDirectories(@"E:\Win7Desktop\项目\Xiaguang\项目");

            foreach (string dir in dirs)
            {
                MyFolder node = new MyFolder()
                                            {
                                                FloderName = dir.Replace(rootFolder.FullPath, "").TrimStart('\\'),
                                                FullPath = dir
                                            };
                rootFolder.Children.Add(node);


                PopFolder(node, dir); 

            }

            return rootFolder; 
        }

        private void PopFolder(MyFolder parent, string dir)
        {
            string[] dirs = System.IO.Directory.GetDirectories(dir);
            foreach (string dirTemp in dirs)
            {
                MyFolder node = new MyFolder()
                {
                    FloderName = dirTemp.Replace(parent.FullPath, "").TrimStart('\\'),
                    FullPath = dirTemp
                }
                                        ;
                parent.Children.Add(node);

                PopFolder(node, dirTemp); 
            }
        }
    }
}
